package com.xzy.controller;

import com.xzy.pojo.ApplyAuthor;
import com.xzy.pojo.VoteUser;
import com.xzy.service.ApplyAuthorService;
import com.xzy.service.TopicVoteService;
import com.xzy.service.VoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ApplyAuthorController {
    @Autowired
    VoteUserService voteUserService;
    @Autowired
    TopicVoteService topicVoteService;
    @Autowired
    private ApplyAuthorService applyAuthorService;

    @RequestMapping("/user/ApplyAuthor")
    public String ApplyAuthor(HttpSession session, Model model){
        VoteUser voteUser= (VoteUser) session.getAttribute("loginUser");
        model.addAttribute("voteUserName",voteUser.getUsername());
        return "ApplyAuthor";
    }
    @RequestMapping("/user/ApplyAuthor/{username}")
    public String ApplyAuthor(@PathVariable String username, Model model){
        int res=0;
        try{
            if((res=this.applyAuthorService.insertAgreeAuthor(username))>0){
                model.addAttribute("AgreeAuthor","用户："+username+"申请成为出题人成功，等待超级管理员通过");
            }else {
                model.addAttribute("AgreeAuthor","用户："+username+"申请成为出题人失败，已经申请过了");
            }
        }catch (Exception e){
            model.addAttribute("AgreeAuthor","用户："+username+"申请成为出题人失败，已经申请过了");
        }
        return "forward:/user/ApplyAuthor";
    }
//    超级管理员查看申请人页面,这里开始就需要权限才可以访问了
    @RequestMapping("/user/AgreeAuthor/to")
    public String AgreeAuthor(Integer nowApplyAuthorPage,Model model,HttpSession session){
        if(nowApplyAuthorPage<=0){
            nowApplyAuthorPage=1;
        }
        List<ApplyAuthor> applyAuthors = this.applyAuthorService.selectApplyAuthorPage((nowApplyAuthorPage - 1) * 5, 5);
        model.addAttribute("applyAuthors",applyAuthors);
        model.addAttribute("nowApplyAuthorPage",nowApplyAuthorPage);
        return "agreeAuthor";
    }

    @RequestMapping("/user/AgreeAuthor/Yes")
    public synchronized String AgreeAuthorYes(String username,Model model,int nowApplyAuthorPage){
        try {
            int res=this.applyAuthorService.updateAgreeAuthorYes(username);
            if(res>0){
                res=this.voteUserService.updateVoteByUsernameYes(username);
                if(res>0){
                    model.addAttribute("AgreeAuthor","超级管理员同意"+username+"成为出题人");
                }else {
                    model.addAttribute("AgreeAuthor","操作失败");
                }
            }
        }catch (Exception e){
            model.addAttribute("AgreeAuthor","操作失败");
        }
        return "forward:/user/AgreeAuthor/to?nowApplyAuthorPage="+nowApplyAuthorPage;
    }

    @RequestMapping("/user/AgreeAuthor/No")
    public synchronized String AgreeAuthorNo(String username,Model model,int nowApplyAuthorPage){
        try {
            int res=this.applyAuthorService.updateAgreeAuthorNo(username);
            if(res>0){
                res=this.voteUserService.updateVoteByUsernameNo(username);
                if(res>0){
                    model.addAttribute("AgreeAuthor","超级管理员拒绝"+username+"成为出题人");
                }else {
                    model.addAttribute("AgreeAuthor","操作失败");
                }
            }
        }catch (Exception e){
            model.addAttribute("AgreeAuthor","操作失败");
        }
        return "forward:/user/AgreeAuthor/to?nowApplyAuthorPage="+nowApplyAuthorPage;
    }
}
